<?php

namespace Tests\Feature;

use Tests\TestCase;

class BookmarkApiTest extends TestCase
{
    /**
     * @test
     */
    public function canCreateABookmark()
    {
        $data = [
            'title' => $this->faker->sentence,
            'description' => $this->faker->paragraph,
            'url' => $this->faker->url,
        ];

        $this->json('POST', route('bookmarks.store'), $data)
            ->assertStatus(201)
            ->assertJson($data);
    }

    /**
     * @test
     */
    public function canCreateABookmarkGivenURLOnly()
    {
        $data = ['url' => 'https://example.com'];

        $this->json('POST', route('bookmarks.store'), $data)
            ->assertStatus(201)
            ->assertJson($data);
    }

    /**
     * @test
     */
    public function cannotCreateABookmarkWithMissingRequiredFields()
    {
        $data = [
            'url' => null,
        ];

        $this->json('POST', route('bookmarks.store'), $data)
            ->assertJsonStructure(['error'])
            ->assertStatus(400);
    }

    /**
     * @test
     */
    public function canEditABookmark()
    {
        $bookmark = factory(\App\Models\Bookmark::class)->create();
        $data = [
            'title' => $this->faker->sentence,
            'description' => $this->faker->paragraph,
            'url' => $this->faker->url,
        ];

        $this->json('PUT', route('bookmarks.update',
            ['id' => $bookmark->id]), $data)
            ->assertStatus(200)
            ->assertJson($data);
    }

    /**
     * @test
     */
    public function canPartialEditABookmark()
    {
        $bookmark = factory(\App\Models\Bookmark::class)->create();
        $data = [
            'description' => $this->faker->paragraph,
        ];

        $this->json('PATCH', route('bookmarks.update',
            ['id' => $bookmark->id]), $data)
            ->assertStatus(200)
            ->assertJson($data);
    }

    /**
     * @test
     */
    public function canShowABookmark()
    {
        $bookmark = factory(\App\Models\Bookmark::class)->create();
        $this->json('GET', route('bookmarks.show', ['id' => $bookmark->id]))
            ->assertStatus(200)
            ->assertJson($bookmark->toArray());
    }

    /**
     * @test
     */
    public function cannotShowANonExistentBookmark()
    {
        $this->json('GET', route('bookmarks.show', ['id' => 1]))
            ->assertJsonStructure(['error'])
            ->assertStatus(404);
    }

    /**
     * @test
     */
    public function canShowAllBookmarks()
    {
        $bookmarks = factory(\App\Models\Bookmark::class, 10)->create();
        $this->json('GET', route('bookmarks.index'))
            ->assertStatus(200)
            ->assertJson($bookmarks->toArray());
    }

    /**
     * @test
     */
    public function canDeleteABookmark()
    {
        $bookmark = factory(\App\Models\Bookmark::class)->create();
        $this->json('DELETE', route('bookmarks.destroy', ['id' => $bookmark->id]))
            ->assertStatus(204);
    }

    /**
     * @test
     */
    public function cannotDeleteANonExistentBookmark()
    {
        $this->json('DELETE', route('bookmarks.destroy', ['id' => 1]))
            ->assertJsonStructure(['error'])
            ->assertStatus(400);
    }
}
